<template>
  <Layer :layer="layer" @confirm="submit" ref="layerDom">
    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px" style="margin-right: 30px">
      <el-form-item label="标签名" prop="name">
        <el-input v-model="form.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="标签颜色" prop="color">
        <el-input v-model="form.color" placeholder="请输入名称">
          <template #append>
            <el-color-picker v-model="form.color" :predefine="predefineColors" @active-change="activeChange"/>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="别名" prop="alias">
        <el-input v-model="form.alias" placeholder="请输入别名"></el-input>
      </el-form-item>
      <el-form-item label="封面图" prop="thumbnail">
        <el-input v-model="form.thumbnail" placeholder="请输入封面图地址"></el-input>
      </el-form-item>
    </el-form>
  </Layer>
</template>

<script lang="ts" setup>
import type {LayerType} from "@/components/layer/index.vue";
import Layer from "@/components/layer/index.vue";
import {Ref, ref} from "vue";
import type {ElFormItemContext} from "element-plus/lib/el-form/src/token";
import {addByUser, editByUser} from "@/api/article/tag";
import {ElMessage} from "element-plus";

const props = defineProps({
  layer: {
    type: Object,
    default() {
      return {
        show: false,
        title: "",
        showButton: true,
      };
    },
  },
});
const predefineColors = ref([
  '#ff4500',
  '#ff8c00',
  '#ffd700',
  '#90ee90',
  '#00ced1',
  '#1e90ff',
  '#c71585',
  'rgba(255, 69, 0, 0.68)',
  'rgb(255, 120, 0)',
  'hsv(51, 100, 98)',
  'hsva(120, 40, 94, 0.5)',
  'hsl(181, 100%, 37%)',
  'hsla(209, 100%, 56%, 0.73)',
  '#c7158577',
])
const emits = defineEmits(["getTableData"]);
const ruleForm: Ref<ElFormItemContext | null> = ref(null);
const layerDom: Ref<LayerType | null> = ref(null);
let form = ref({
  name: "",
  color: '#409EFF',
  alias: '',
  thumbnail: '',
});
const rules = {
  name: [{required: true, message: "请输入姓名", trigger: "blur"}],
};

init();

function init() {
  // 用于判断新增还是编辑功能
  if (props.layer.row) {
    form.value = JSON.parse(JSON.stringify(props.layer.row)); // 数量量少的直接使用这个转
  } else {
  }
}

//查询当前文章的标签
const initTag = () => {

}

const submit = () => {
  ruleForm.value.validate((valid: boolean) => {
    if (valid) {
      let params = form.value;
      if (props.layer.row) {
        updateForm(params);
      } else {
        addForm(params);
      }
    } else {
      return false;
    }
  });
};

// 新增提交事件
const addForm = (params: object) => {
  addByUser(params).then((res) => {
    ElMessage({
      type: "success",
      message: "新增成功",
    });
    props.layer.show = false;
    emits("getTableData", false);
  });
};

// 编辑提交事件
const updateForm = (params: object) => {
  editByUser(params).then((res) => {
    ElMessage({
      type: "success",
      message: "编辑成功",
    });
    props.layer.show = false;
    emits("getTableData", false);
  });
};
//选择当前颜色
const activeChange = (color: string) => {
  form.value.color = color
}
</script>

<style lang="scss" scoped>
::v-deep .el-input-group__append {
  padding: 0;
  border: 0
}
</style>